package com.ct.book.controller;

import com.ct.book.constants.Constants;
import com.ct.book.model.*;
import com.ct.book.service.BookService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    @RequestMapping("/addBook")
    public String addBook (BookInfo bookInfo) {
        log.info("添加图书:{}", bookInfo);
        if (!StringUtils.hasLength(bookInfo.getBookName())
                || !StringUtils.hasLength(bookInfo.getAuthor())
                || bookInfo.getCount() == null
                || bookInfo.getPrice() == null
                || !StringUtils.hasLength(bookInfo.getPublish())
                || bookInfo.getStatus() == null) {
            return "输入参数不合法, 请重新输入!";
        }
        try {
            bookService.addBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("添加图书失败: ", e);
            return e.getMessage();
        }
    }

    @RequestMapping("/getListByPage")
    public Result<PageResult<BookInfo>> getListByPage (PageRequest pageRequest, HttpSession session) {
        log.info("获取图书列表, pageRequest:{}", pageRequest);
//         判断用户是否登录
//        if (session.getAttribute(Constants.SESSION_USER_KEY) == null) {
//            return Result.unlogin();
//        }
//        UserInfo userInfo = (UserInfo) session.getAttribute(Constants.SESSION_USER_KEY);
//        if (userInfo == null || userInfo.getId() < 0) {
//            return Result.unlogin();
//        }
        PageResult<BookInfo> pageResult = bookService.getBookListByPage(pageRequest);
        return Result.success(pageResult);
    }

    @RequestMapping("/queryBookById")
    public BookInfo queryBookById (Integer bookId) {
        if (bookId == null || bookId <= 0) {
            return new BookInfo();
        }
        BookInfo bookInfo = bookService.queryBookById(bookId);
        log.info("bookInfo:{}", bookInfo);
        return bookInfo;
    }

    @RequestMapping("/updateBook")
    public String updateBook (BookInfo bookInfo) {
        log.info("修改图书:{}", bookInfo);
        try {
            bookService.updateBook(bookInfo);
            return "";
        } catch (Exception e) {
            log.error("修改图书失败: ", e);
            return e.getMessage();
        }
    }

    @RequestMapping("/deleteBook")
    public String deleteBook (Integer bookId) {
        log.info("删除图书:{}", bookId);
        try {
            Integer result = bookService.deleteById(bookId);
            if (result < 1) {
                return "删除图书失败, 请联系管理员!";
            }
            return "";
        } catch (Exception e) {
            log.error("图书删除发生异常", e);
            return "删除图书失败, 请联系管理员!";
        }
    }

    @RequestMapping("/batchDeleteBook")
    public String batchDeleteBook(@RequestParam List<Integer> ids) {
        log.info("批量删除图书, id{}", ids);
        try {
            Integer result = bookService.batchDeleteById(ids);
            if (result < 1) {
                return "批量删除图书失败, 请联系管理员!";
            }
            return "";
        } catch (Exception e) {
            log.error("发生异常e", e);
            return "批量删除图书失败, 请联系管理员";
        }
    }


}
